<?php
$section = "booking";
$titre = _LB_BOOKING;
$link = $section;
Session::setBackLink($link);
//create cfer
$act = Request::getIntNumber('act');
$bsID = Request::getIntNumber('sID');
$sesID = Request::getIntNumber('sesID');
if($bsID > 0){
	$_SESSION['bookingData'] = NULL;
	unset($_SESSION['bookingData']);
}
$cfer = new Cfer(array(
	_LB_HOME =>  APPLICATION_URL_FO."/",
    _LB_BOOKING => APPLICATION_URL_FO."/booking/"
));
$smarty->assign('selectedDATE', date("Y-m-d"));
if(isset($_GET['sesID']) && isset($_SESSION['bookingData'])  && $_SESSION['bookingData'] != "" && is_array(unserialize($_SESSION['bookingData']))){
	$bookingData = unserialize($_SESSION['bookingData']);
	if(isset($bookingData[$sesID])){
		$smarty->assign('selectedService', $bookingData[$sesID]['SERVICES']);
		$smarty->assign('selectedDATE', $bookingData[$sesID]['DATE']);
		$smarty->assign('selectedTIME', $bookingData[$sesID]['TIME']);
	}
}
//if action = UPDATE or CREATE
if ($act == ACT_VALID_EDIT)
{
	
	$booking = new Booking();
	if(!isMemberLoggedIn()){
		$booking->FIRST_NAME = Request::get("FIRST_NAME");
		$booking->LAST_NAME = Request::get("LAST_NAME");
		$booking->PHONE = Request::get("PHONE");
		$booking->EMAIL = Request::get("EMAIL");
	}
	else{
		$booking->ID_MEMBER = $_SESSION['memberID'];
	}
	$booking->DATE_CREATION = date("Y-m-d H:i:s");
	$booking->IS_REMINDED = 0;
	$booking->STATUS = 0;
	$time = Request::get("TIME");
	$date = Request::get("DATE");
	$date = date_to_date_sql($date);
	$booking_date = $date. " {$time}";
	$techID = Request::getIntNumber("TECHNICIAN");
	$service = $_POST['SERVICE'];
    $arr = array(
		"DATE" =>$date,
		"TIME" =>$time,
		"SERVICES" => $service
	);
	$arrData = array();
	$ind = 0;
	if(isset($_SESSION['bookingData'])){
		$tmp = $_SESSION['bookingData'];
		$arrData = unserialize($tmp);
		$ind = count($arrData);
	}
	if(isset($_GET['sesID']) && isset($_SESSION['bookingData'])  && $_SESSION['bookingData'] != "" && is_array(unserialize($_SESSION['bookingData']))){
		$bookingData = unserialize($_SESSION['bookingData']);
		if(isset($bookingData[$sesID])){
			$arrData[$sesID] = $arr;
		}
		else{
			$arrData[$ind] = $arr;
		}
	}
	else{
		$arrData[$ind] = $arr;
	}
	
	$_SESSION['bookingData'] = serialize($arrData);
	redirect(APPLICATION_URL_FO . "/booking/preview/");
	exit();
}
$service = new Service_Booking();
$serviceData = $service->parseService2Array();

//$a = Booking::checkAvailableBooking("27/03/2014", "1,2,3");
Session::setBackCfer($link, $cfer->getCferArray());
$smarty->assign('cfer', $cfer);
$smarty->assign('sData', $serviceData);
$smarty->assign('titre', $titre);
$smarty->assign('bsID', $bsID);
$smarty->assign('currentDATE', date("Y-m-d"));

$smarty->assign('bookedID', 0);
$smarty->assign('bookedSerialize', isset($_SESSION['bookingData'])?$_SESSION['bookingData']:"");
$smarty->display("booking_form.html");
?>
